﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;


namespace ServeNHL.Models
{
    

    [MetadataType(typeof(familyMetaData))]
    public partial class FAMILY
    {

        /// <summary>
        /// inserts or updates db based on family ID
        /// </summary>
        /// <param name="family"></param>
        /// <returns>fmailyID</returns>
        public int saveFamily(FamilyViewModel family)
        {
            int familyID = 0;

            if(family.family.FAMILY_ID >= 0)
            {
                updateFamily(family.family);
            }
            else
            {
                insertFamily(family.family);
            }

            familyID = family.family.FAMILY_ID;

            return familyID;
        }//end of public int saveFamily(FamilyViewModel family)


        /// <summary>
        /// inserts new Family into DB
        /// </summary>
        /// <param name="family"></param>
        /// <returns>familyID</returns>
        public int insertFamily(FAMILY family)
        {
            int familyID = 0;

            using(var dbconn = new fclick_dbEntities())
            {
                var tempFam = new FAMILY()
                {
                    FAMILY_NAME = family.FAMILY_NAME,
                    FAMILY_HEAD1 = family.FAMILY_HEAD1,
                    FAMILY_HEAD2 = family.FAMILY_HEAD2,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate()
                };

            dbconn.FAMILies.Add(tempFam);
            dbconn.SaveChanges();

            familyID = tempFam.FAMILY_ID;

            }//end of using(var dbconn = new fclick_dbEntities())

            return familyID;
        }//end of public int insertFamily(FAMILY family)

        /// <summary>
        /// updates family to DB
        /// </summary>
        /// <param name="family"></param>
        /// <returns>familyID</returns>
        public int updateFamily(FAMILY family)
        {
            int familyID = 0;

            using(var dbconn = new fclick_dbEntities())
            {
                var tempFam = new FAMILY();

                if (!String.IsNullOrEmpty(family.FAMILY_NAME))
                {
                    tempFam.FAMILY_NAME = family.FAMILY_NAME;
                }

                //Need to have at least 1 family head, right? But not a second- actually both nullable
               
                tempFam.FAMILY_HEAD1 = family.FAMILY_HEAD1;
                
                
                tempFam.FAMILY_HEAD2 = family.FAMILY_HEAD2;

                dbconn.FAMILies.Add(tempFam);
                dbconn.SaveChanges();


                familyID = family.FAMILY_ID;
            }//end of using(var dbconn = new fclick_dbEntities())

            return familyID;

        }//end of public int updateFamily(FAMILY family)

    }//end of partial class family
}